import time
from Pages.ImageBuildPage import ImageBuild
from PublicFunc.assertion import assertion_equal
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class ImageCase:
    def __init__(self, config, logger):
        self.service = ChromeService(executable_path=ChromeDriverManager().install())
        self.driver = webdriver.Chrome(service=self.service)
        self.config = config
        self.logger = logger

    # 流水线页面信息检查
    def test_check_info(self):
        self.logger.logging("info", '=========添加流水线页面信息检查=======')
        case1_description = '查看页面信息，流水线列表是否生效'
        case2_description = '查看页面信息，用户未登录时，添加流水线按钮是否隐藏'
        case3_description = '查看页面信息，搜索框显示'
        case4_description = '查看页面信息，筛选下拉框显示'
        image_page = ImageBuild(self.driver)
        image_page.get_url(self.config.BASIC_URL + "images")
        WebDriverWait(self.driver, 10).until(EC.url_contains(self.config.BASIC_URL + "images"))
        page_list = image_page.check_info()
        self.logger.logging("info", assertion_equal(page_list[0], '流水线列表', case1_description))
        self.logger.logging("info", assertion_equal(page_list[1], '用户未登录，添加流水线按钮不显示', case2_description))
        self.logger.logging("info", assertion_equal(page_list[2], '显示输入框', case3_description))
        self.logger.logging("info", assertion_equal(page_list[3], '显示筛选下拉框', case4_description))
        self.logger.logging("info", '========================================')

    # 添加流水线按钮及页面信息检查
    def test_pipeline_button_check(self):
        case_description = '添加流水线按钮功能检查'
        image_page = ImageBuild(self.driver)
        image_page.get_url(self.config.BASIC_URL + "images")
        time.sleep(3)
        image_page.user_login(user_name=self.config.NAME, password=self.config.PASSWORD)
        time.sleep(15)
        image_page.add_pipeline()
        time.sleep(3)
        div_info = image_page.pipeline_check()
        self.logger.logging("info", assertion_equal(div_info, '添加流水线弹窗显示正常', case_description))

        self.logger.logging("info", '=========添加流水线按钮页面信息检查=======')
        case_1_description = '流水线类型信息检查'
        case_2_description = '流水线分组信息检查'
        case_3_description = '流水线名称信息检查'
        case_4_description = '镜像格式信息检查'
        case_5_description = '架构信息检查'
        info_list = image_page.pipeline_info_check()
        self.logger.logging("info", assertion_equal(info_list[0], '流水线类型', case_1_description))
        self.logger.logging("info", assertion_equal(info_list[1], '流水线分组', case_2_description))
        self.logger.logging("info", assertion_equal(info_list[2], '流水线名称', case_3_description))
        self.logger.logging("info", assertion_equal(info_list[3], '镜像格式', case_4_description))
        self.logger.logging("info", assertion_equal(info_list[4], '架构', case_5_description))
        self.logger.logging("info", '========================================')

    def __str__(self):
        self.logger.logging("info", '*************登录模块***************')

    # 执行用例
    def run_test(self):
        self.__str__()
        self.driver.maximize_window()
        self.test_check_info()
        time.sleep(3)
        self.test_pipeline_button_check()
        time.sleep(1)
        self.driver.quit()
